package com.zrs.gmall.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrs.gmall.common.cache.GmallCache;
import com.zrs.gmall.product.mapper.BaseCategoryViewMapper;
import com.zrs.gmall.product.model.BaseCategoryView;
import com.zrs.gmall.product.service.BaseCategoryViewService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BaseCategoryViewServiceServiceImpl
        extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView>
        implements BaseCategoryViewService {
    /**
     * 查询所有分类列表 分类嵌套结果:一级分类分类对象中包含二级分类集合;在二级分类对象中包含三级分类集合
     *
     * @return
     */
    @Override
    @GmallCache(prefix = "baseCategoryList")
    public List<JSONObject> getBaseCategoryList() {
        //1.构建所有一级分类集合对象
        List<JSONObject> resultList = new ArrayList<>();

        //2.查询分类视图获取所有的分类集合
        List<BaseCategoryView> allCategoryList = this.list();

        //3.对所有分类集合进行分组:根据一级分类ID分组
        Map<Long, List<BaseCategoryView>> category1ListMap =
                allCategoryList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        int index = 1;
        //4.遍历分组后Map处理一级分类数据
        for (Map.Entry<Long, List<BaseCategoryView>> category1Entry : category1ListMap.entrySet()) {
            JSONObject category1 = new JSONObject();
            //4.1 获取一级分类ID
            Long category1Id = category1Entry.getKey();
            //4.2 获取一级分类名称
            String category1Name = category1Entry.getValue().get(0).getCategory1Name();
            category1.put("index", index++);
            category1.put("categoryId", category1Id);
            category1.put("categoryName", category1Name);

            //5.处理当前一级分类中二级分类
            Map<Long, List<BaseCategoryView>> category2ListMap = category1Entry.getValue().stream()
                    .collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            //5.1 遍历二级分类Map 获取二级分类ID以及名称

            List<JSONObject> category2JsonList = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> category2Entry : category2ListMap.entrySet()) {
                //5.1.1 构建二级分类JSon对象
                JSONObject category2 = new JSONObject();
                //5.1.2 获取二级分类ID以及名称
                Long category2Id = category2Entry.getKey();
                String category2Name = category2Entry.getValue().get(0).getCategory2Name();
                //5.1.3 封装二级分类JSON对象
                category2.put("categoryId", category2Id);
                category2.put("categoryName", category2Name);
                category2JsonList.add(category2);

                //6.处理当前二级分类中包含三级分类
                List<JSONObject> category3JsonList = new ArrayList<>();
                List<BaseCategoryView> category3List = category2Entry.getValue();
                //6.1 遍历三级分类集合 构建三级分类对象;将三级分类集合 放入二级分类对象 categoryChild属性中
                for (BaseCategoryView baseCategoryView : category3List) {
                    JSONObject category3 = new JSONObject();
                    category3.put("categoryId", baseCategoryView.getCategory3Id());
                    category3.put("categoryName", baseCategoryView.getCategory3Name());
                    category3JsonList.add(category3);
                }

                //将处理后三级分类加入当前二级分类中
                category2.put("categoryChild", category3JsonList);
            }
            // 将二级分类集合加入到一级分类对象 categoryChild属性
            category1.put("categoryChild", category2JsonList);
            //7.将一级分类对象加入总结果集合中
            resultList.add(category1);
        }
        return resultList;
    }
}